<template>
    <div>
        <el-form @submit.prevent="changePassword">
            <el-form-item prop="password">
                <el-input type="password" id="oldPassword" v-model="oldPassword" required placeholder="请输入旧密码"
                    show-password @blur="a" />
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" id="newPassword" v-model="newPassword" required minlength="6"
                    placeholder="请输入新密码" show-password />
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" id="confirmPassword" v-model="confirmPassword" required placeholder="请输入确认新密码"
                    show-password @blur="b" />
            </el-form-item>
            <el-button v-show="bool" style="width: 100%;background-color:orange;" @click="c">修改个人密码</el-button>
        </el-form>
    </div>


</template>

<script>

export default {
    data() {
        return {
            oldPassword: '',
            newPassword: '',
            confirmPassword: '',
            data: JSON.parse(sessionStorage.getItem('User')),
            bool: true,
        };
    },
    methods: {
        a() {
            this.axios({
                method: 'put',
                url: 'Users/EditOldPwd',
                params: { id: this.data.UserId, pwd: this.oldPassword },
            }).then(res => {
                console.log('res: ', res);
                if (res.data.code > 0) {
                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    });

                    this.bool = true;

                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'error'
                    })

                    this.bool = false;
                }
            })
                .catch(err => {
                    console.log('err: ', err);

                })
        },
        b() {
            if(this.newPassword != this.confirmPassword){
                this.$message({
                        message: '新密码与确认新密码不一致!',
                        type: 'error'
                    });

                    this.bool = false;
            }else{
                this.bool = true;

            }
        },
        c() {
            this.axios({
                method: 'put',
                url: 'Users/EditNewPwd',
                params: { id: this.data.UserId, pwd: this.newPassword },
            }).then(res => {
                console.log('res: ', res);
                if (res.data.code > 0) {
                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    });

                    this.$emit('update:dialogVisible',false);
                    this.oldPassword=null;
                    this.newPassword=null;
                    this.confirmPassword=null;

                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'error'
                    })
                }
            })
                .catch(err => {
                    console.log('err: ', err);

                })
        }

    }
};
</script>